//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.IO;
using System.Web.Caching;
using CommunityServer.Components;


namespace CommunityServer.Discussions.Components {

    // *********************************************************************
    //  PrivateMessages
    //
    /// <summary>
    /// This class contains methods for sending and managing private messages
    /// between users
    /// </summary>
    // ***********************************************************************/
    public class PrivateMessages {


		internal static void FormatPrivateMessagePost(ForumPost post)
		{
			// Convert the subject to the formatted version before adding the post
			//
			post.Subject = Globals.HtmlEncode(post.Subject);
			post.FormattedBody = post.Body;

			CSEvents.PrePost(post,ObjectState.Create,ApplicationType.Forum);
			CSEvents.BeforePost(post,ObjectState.Create,ApplicationType.Forum);
		}

        // *********************************************************************
        //  AddPrivateMessage
        //
        /// <summary>
        /// Adds users to a private message
        /// </summary>
        /// <param name="users">An int array of UserIDs the message belongs to</param>
        /// <param name="threadID">The message to grant access to</param>
        /// 
        // ***********************************************************************/
        public static ForumPost AddPrivateMessagePost (ForumPost post, User author, ArrayList users) {
			FormatPrivateMessagePost(post);

            // Are we tracking the post IP address?
            //
        	CSContext cntx = CSContext.Current;
        	if (cntx.SiteSettings.EnableTrackPostsByIP)
                post.UserHostAddress = cntx.Context.Request.UserHostAddress;

            // Create Instance of the CommonDataProvider
            //
            ForumDataProvider dp = ForumDataProvider.Instance();

            ForumPost newPost = dp.AddPost(post, author.UserID);

			AddRecipients(users, newPost);

			CSEvents.AfterPost(newPost,ObjectState.Create,ApplicationType.Forum);
			

            return newPost;

        }

		public static void AddRecipients (ArrayList users, ForumPost post) {
			// Create Instance of the CommonDataProvider
			//
			ForumDataProvider dp = ForumDataProvider.Instance();

			dp.CreatePrivateMessage(users, post.ThreadID);

			Emails.PrivateMessageNotifications(post, users);

		}

		public static string GetFormattedRecipients (int threadID, User replyTo, int authorId) {
			HybridDictionary d = GetRecipients(threadID, authorId);
			string users = null;

			// ensure we add the person we are replying to
			if ((d[replyTo.UserID] == null) && (replyTo.UserID != authorId))
				d.Add(replyTo.UserID, replyTo.Username);

			foreach (string user in d.Values)
				users += user + ";";

			if (users == null)
				return "";

			return users.Trim(';');
		}

		public static HybridDictionary GetRecipients (int threadID, int author) {
			// Create Instance of the CommonDataProvider
			//
			ForumDataProvider dp = ForumDataProvider.Instance();

			HybridDictionary users = dp.GetPrivateMessageRecipients(threadID);

			users.Remove( author );

			return users;

		}

        public static void DeletePrivateMessages (int userID, ArrayList deleteList) {

            // Create Instance of the CommonDataProvider
            //
            ForumDataProvider dp = ForumDataProvider.Instance();

            dp.DeletePrivateMessage(userID, deleteList);

        }

    }
}
